<?php
namespace app\jobmanagement\controller;


use app\constdir\ApiConst;
use app\jobmanagement\service\Chart as ChartService;
use think\Controller;

/**
 * Class Chart
 * @package app\jobmanagement\controller
 * @description 图片相关API
 */
//class Chart extends Base
class Chart extends Controller
{


    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin:*');//允许所有来源访问
        header('Access-Control-Allow-Method:POST,GET');//允许访问的方式

    }

    /**
     * @auther zlq
     * @create_time 2020/6/15 19:30
     * @description 展示页面
     * @return mixed
     */
    public function chartView()
    {
        return $this->fetch('index');
    }

    /**
     * @auther zlq
     * @create_time 2020/6/15 19:29
     * @description 图文显示所有数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chartData()
    {
        try {
            $sChart = new ChartService();
            $amountList = $sChart->todayAmount();
            $workerList = $sChart->userCount();
            $task = $sChart->getTodayFinishTask();
            $week = $sChart->getLastWeekData();
            return json([
                'status' => 1,
                'msg' => 'success',
                'data' => [
                    'amount' => $amountList,
                    'worker' => $workerList,
                    'task' => $task,
                    'week' => $week,
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'status' => 0,
                'msg' => 'error',
                'data' => []
            ]);
        }


    }

    /**
     * @introduction:返回所有生产车间今日所支付的计件费用
     * @return \think\response\Json
     * @author:cxh
     * @date: 2020/6/15 17:30
     */
    public function todayAmount(){
        $sChart = new ChartService();
        $amountList = $sChart->todayAmount();


        return json(['status' => 1, 'msg' => 'success', 'data' => $amountList]);
    }


    /**
     * @introduction:从任务单形成数据中读取今天使用人数
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/6/15 17:18
     */
    public function userCount()
    {
        $sChart = new ChartService();
        $workerList = $sChart->userCount();

        return json(['status' => 1, 'msg' => 'success', 'data' => $workerList]);
    }

    /**
     * @auther zlq
     * @create_time 2020/6/15 14:22
     * @description 获取每日任务量表格
     * @return \think\response\Json
     */
    public function getTodayFinishTask()
    {
        $chart = new ChartService();
        $data = $chart->getTodayFinishTask();
        return json(['status' => 1, 'msg' => 'success', 'data' => $data]);
    }

    /**
     * @auther zlq
     * @create_time 2020/6/15 15:47
     * @description 获取上周人员金额数据
     * @return \think\response\Json
     */
    public function getLastWeekData()
    {
        $chart = new ChartService();
        $data = $chart->getLastWeekData();
        return json(['status' => 1, 'msg' => 'success', 'data' => $data]);

    }

    /**
     * @auther zlq
     * @create_time 2020/5/21 22:32
     * @description 导出数据到Excel
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exportCapital()
    {
        $ids = input('ids', '');
        $ids = trim($ids, '-');
        $capitalService = new ChartService();
        if ($ids) {
            $param['ids'] = $ids;
            $data = $capitalService->getCapitalListExcel(['ids' => $ids], 1);
        } else {
            $param['capital_type'] = input('capital_type', 0);
            $param['capital_status'] = input('capital_status', 0);
            $param['user'] = input('user', 0);
            $data = $capitalService->getCapitalListExcel($param, 2);
        }
        if (empty($data)) {
            $this->error('数据为空,无法导出');
        }
        $capital_type = config('dic.capital_type');
        $capital_status = config('dic.capital_status');
        // 导出表格
        require_once  EXTEND_PATH . 'PHPExcel/Classes/PHPExcel.php';
        require_once  EXTEND_PATH . 'PHPExcel/Classes/PHPExcel/Worksheet/Drawing.php';
        $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        $objReader->setIncludeCharts(TRUE);
        //加载模板
        $objPHPExcel = $objReader->load( EXTEND_PATH . "PHPExcel/Examples/templates/capital_wap.xlsx");
        $row = $baseRow = 1;
        $column = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
        $style_array = [
            'borders' => [
                'allborders' => [
                    'style' => \PHPExcel_Style_Border::BORDER_THIN
                ]
            ]
        ];
        if($data){
            foreach($data as $r => $dataRow) {
                $row++ ;
                $objPHPExcel->getActiveSheet()
                    ->setCellValue('A' . $row, ($r+1))
                    ->setCellValue('B' . $row, $dataRow['id'])
                    ->setCellValue('C' . $row, $capital_type[$dataRow['type']])
                    ->setCellValue('D' . $row, $dataRow['amount'])
                    ->setCellValue('E' . $row, $capital_status[$dataRow['status']])
                    ->setCellValue('F' . $row, get_user_name($dataRow['dd_user_id']))
                    ->setCellValue('G' . $row, $dataRow['create_time']);
                $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(20);
                foreach ($column as $item) {
                    $objPHPExcel->getActiveSheet()->getStyle($item . $row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle($item . $row)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle($item . $row)->applyFromArray($style_array);
                }
            }
        }
        ob_end_clean();//清除缓冲区,避免乱码
        // Redirect output to a client's web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.date("Y-m-d H:i:s").'记录.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->setIncludeCharts(TRUE);
        $objWriter->save('php://output');
    }
}